﻿using EBA.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Linq;
using EBA.Helpers;

namespace EBA.Logging
{
    /// <summary>
    /// xml文档形式log序列化
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static class XmlFileLogSerializer
    {
        static IJsonSerializer Serializer = JsonSerializer.GetInstance();
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Serialize(XElementLog log)
        {
            var content = new XElement("Log",
             new XAttribute("id", log.Id),
             new XAttribute("level", log.Level),
             new XAttribute("name", log.Name),
             new XAttribute("message", log.Message),
             new XAttribute("addedtime", log.AddedTime));
            return content.ToString();
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <typeparam name="T">自定义类型</typeparam>
        /// <param name="log"></param>
        /// <returns></returns>
        public static string Serialize<T>(XElementLog log)
        {
            var content = new XElement("Log",
             new XAttribute("id", log.Id),
             new XAttribute("level", log.Level),
             new XAttribute("name", log.Name),
             new XAttribute("message", log.Message),
             new XAttribute("addedtime", log.AddedTime),
             new XAttribute("data", Serializer.Serialize<T>((T)log.Data)));
            return content.ToString();
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static XElementLog Deserialize(string str)
        {
            var content = XElement.Parse(str);
            var log = new XElementLog();
            log.Id = content.GetAttributeValue("id");
            log.Level = LoggingHelper.ConvertToLogLevel(content.GetAttributeValue("level"));
            log.AddedTime = DateTime.Parse(content.GetAttributeValue("addtime"));
            log.Name = content.GetAttributeValue("name");
            log.Message = content.GetAttributeValue("message");
            log.Data = content.GetAttributeValue("data");
            return log;
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T">自定义类型</typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static XElementLog Deserialize<T>(string str)
        {
            var content = XElement.Parse(str);
            var log = new XElementLog();
            log.Id = content.GetAttributeValue("id");
            log.Level = LoggingHelper.ConvertToLogLevel(content.GetAttributeValue("level"));
            log.AddedTime = DateTime.Parse(content.GetAttributeValue("addtime"));
            log.Name = content.GetAttributeValue("name");
            log.Message = content.GetAttributeValue("message");
            log.Data = Serializer.Deserialize<T>(content.GetAttributeValue("data"));
            return log;
        }

    }
}
